import {config as base_url} from '../config.js'
class Http{
  request(params){
    // 
    let method = params.method ? params.method : "GET"
    let url = params.url ? base_url.api_base_url + params.url : base_url.api_base_url +'/latest.json'
    wx.request({
      url: url,
      method: method,
      data: params.data,
      header: {
        "content-type": "application/json",
        "appkey": base_url.appkey
      },
      success:res=>{
        let code = res.statusCode.toString()
        if (code.startsWith('2')){
          params.success && params.success(res.data)
        }else{
          this._show_error()
        }
      },
      fail:err=>{
        this._show_error()
      }
    })
  }

  _show_error(error_code){
    wx.showToast({
      title:'请求失败',
      icon: 'none',
      duration: 2000
    })
  }
}

export default Http